package com.sundaying.gooway.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.sundaying.gooway.dao.TopClassDao;
import com.sundaying.gooway.entity.TopClass;

@Component("topClassDao")
public class TopClassDaoImpl implements TopClassDao {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public int save(TopClass obj) {
		int result=0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public int update(TopClass obj) {
		int result=0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public int delete(TopClass obj) {
		int result=0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public TopClass load(int id) {
		TopClass topClass = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		topClass = (TopClass) session.load(TopClass.class, id);
		session.getTransaction().commit();
		session.close();
		return topClass;
	}

	@Override
	public List<TopClass> list() {
		List<TopClass> topClassList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery("from TopClass");
		topClassList = (List<TopClass>) query.list();
		session.getTransaction().commit();
		session.close();
		return topClassList;
	}

	@Override
	public List<TopClass> list(String hsql, List<Object> param) {
		List<TopClass> topClassList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hsql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}

		topClassList = (List<TopClass>) query.list();
		session.getTransaction().commit();
		session.close();
		return topClassList;
	}

	@Override
	public List<TopClass> list(String hsql, List<Object> param, int page,
			int rows) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long count(String hql, List<Object> param) {
		// TODO Auto-generated method stub
		return 0;
	}

	
}
